//
//  ByConjurationNewVC.m
//  mijifen_ios
//
//  Created by mc on 2017/10/25.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByConjurationNewVC.h"

// other
#import "ZKSegmentControl.h"

// view
#import "ByViewSweepTable.h"
#import "ByViewAimTable.h"

// vc
#import "ByConjurationSuccessVC.h"

@interface ByConjurationNewVC ()
@property (nonatomic, copy) NSDictionary *infoDic;
@property (nonatomic, assign) NSInteger   selectIndex;
@end

@implementation ByConjurationNewVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"召唤术";
    self.view.backgroundColor = BY_TABLE_COLOR;
    [self createSegment];
}


#pragma mark - 视图初始化
- (void)createSegment{
    // header
    UILabel *headerLabel = [[UILabel alloc] init];
    headerLabel.numberOfLines = 0;
    headerLabel.font = FONT(14);
    headerLabel.textColor = [UIColor colorWithR:194 g:54 b:60];
    [self.view addSubview:headerLabel];
    NSString *infoStr = @"红包只在用户通过微信或支付宝支付时使用\n 红包费用由商家自己承担";
    [headerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        if (@available(iOS 11.0,*)) {
            make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop).offset(15);
        }else{
            make.top.equalTo(self.view.mas_top).offset(15);
        }

    }];
    headerLabel.text = infoStr;
    headerLabel.textAlignment = NSTextAlignmentCenter;
    
    // 主视图
    ZKSegmentControl *control = [[ZKSegmentControl alloc] init];
    [self.view addSubview:control];
    [control mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.top.equalTo(headerLabel.mas_bottom).offset(15);
        make.right.equalTo(self.view.mas_right);
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(- 49 - 60);
        }else{
            make.bottom.mas_equalTo(self.view.mas_bottom).offset(- 49 - 60);
        }
    }];
    control.segmentsArray = @[@"席卷式发放",@"针对性发放"];
    control.segmentSelectTint = BY_TEXT_COLOR;
    control.backgroundColor = BY_TABLE_COLOR;
    
    control.dynamicalAddContentViewBlock = ^(void (^contentViewFetchBlock)(ZKSegmentContentView *contentView), NSInteger selectedIndex) {
        self.selectIndex = selectedIndex;
        __weak typeof(self) weakSelf = self;
        if (selectedIndex == 0) {
            ByViewSweepTable *sweepTable = [[ByViewSweepTable alloc] init];
            if (contentViewFetchBlock) {
                contentViewFetchBlock(sweepTable);
            }
            sweepTable.contentViewDidLoad = ^(NSInteger index) {
                NSLog(@"席卷式---%ld",index);
            };
            sweepTable.contentViewDidAppear = ^(NSInteger index) {
                NSLog(@"席卷式的----%ld",index);
            };
            sweepTable.editBlock = ^(NSDictionary *dic) {
                weakSelf.infoDic = dic;
            };
        }else{
            ByViewAimTable *aimTable = [[ByViewAimTable alloc] init];
            if (contentViewFetchBlock) {
                contentViewFetchBlock(aimTable);
            }
            aimTable.editBlock = ^(NSDictionary *dic) {
                weakSelf.infoDic = dic;
            };
        }
    };
    
    
    // 底部提交
    UIButton *commitButton = [[UIButton alloc] init];
    [self.view addSubview:commitButton];
    [commitButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.view.mas_left);
        make.right.equalTo(self.view.mas_right);
        make.size.mas_equalTo(CGSizeMake(SCREEN_WIDTH, 50));
        if (@available(iOS 11.0,*)) {
            make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom).offset(- 49 - 10);
        }else{
            make.bottom.equalTo(self.view.mas_bottom).offset(- 49 - 10);
        }
    }];
    [commitButton setTitle:@"提交" forState:UIControlStateNormal];
    [commitButton setTitleColor:BY_TEXT_COLOR forState:UIControlStateNormal];
    [commitButton setBackgroundColor:BY_THEME_COLOR];
    [commitButton addTarget:self action:@selector(commitAction:) forControlEvents:UIControlEventTouchUpInside];
}

#pragma mark - 提交
- (void)commitAction:(UIButton *)button{
    // 红包金额
    NSString *price = self.infoDic[@"10"];
    if (price.length == 0) {
        [ByCoreToolAlert showOnlyText:@"请输入红包金额"];
        return;
    }
    // 席卷式发放
    if (self.selectIndex == 0) {
        // 红包个数
        NSString *count = self.infoDic[@"11"];
        if (count.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入红包个数"];
            return;
        }
        // 使用条件
        NSString *condition = self.infoDic[@"12"];
        if (condition.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入使用条件"];
            return;
        }
        // 有效期
        NSString *valid = self.infoDic[@"13"];
        if (valid.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入有效期"];
            return;
        }
        // 备注
        NSString *node = self.infoDic[@"14"];
        NSLog(@"%@",node);
    }
    // 针对性发放
    else{
        // 使用条件
        NSString *condition = self.infoDic[@"11"];
        if (condition.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入使用条件"];
            return;
        }
        
        // 有效期
        NSString *valid = self.infoDic[@"12"];
        if (valid.length == 0) {
            [ByCoreToolAlert showOnlyText:@"请输入有效期"];
            return;
        }
        
        
    }


    ByConjurationSuccessVC *vc = [[ByConjurationSuccessVC alloc] init];
    vc.type = self.selectIndex;
    vc.price = price;
    vc.redCount = self.infoDic[@"11"];
    [self by_normalPush:vc hasNavi:YES];
}

@end
